<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>EOS.IO: Developing Smart Contracts / Decentralized Applications with EOS.IO</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="eos-logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">EOS.IO
   &#160;<span id="projectnumber">DAWN1.1.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Developing Smart Contracts / Decentralized Applications with EOS.IO </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This tutorial is targeted toward those who would like to get started building decentralized applications or smart contracts using the EOS.IO software development kit. It will cover writing contracts/applications in C++ and uploading them to the blockchain. It will not cover how to build or deploy interactive web interfaces.</p>
<p>For the rest of this tutorial we will use the term "contract" to refer to the code that powers decentralized applications.</p>
<h2>Required Background Knowledge</h2>
<p>This tutorial assumes that you have basic understanding of how to use 'eosd' and 'eosc' to setup a node and deploy the example contracts. If you have not yet successfully followed that tutorial, then do that first and come back.</p>
<h3>C / C++ Experience Required</h3>
<p>EOS.IO based blockchains execute user generated applications and code using Web Assembly (WASM). WASM is an emerging web standard with widespread support of Google, Microsoft, Apple, and others. At the moment the most mature toolchain for building applications that compile to WASM is clang/llvm with their C/C++ compiler.</p>
<p>Other toolchains in development by 3rd parties include: Rust, Python, and Solidiity. While these other languages may appear simpler, their performance will likely impact the scale of application you can build. We expect that C++ will be the best language for developing high-performance and secure smart contacts.</p>
<p>If you are not experienced with C/C++, then you should learn the basics first and then come back.</p>
<h3>Linux / Mac OS X Command Line</h3>
<p>The EOS.IO software only officially supports unix environments and building with clang.</p>
<h2>Basics of Smart Contracts / Decentralized Applications</h2>
<p>Fundamentally every contract is a state machine that responds to signed user actions. These contracts are uploaded to the blockchain as pre-compiled Web Assembly (WASM) files with an ABI (Application Binary Interface).</p>
<p>To keep things simple we have created a tool called <code>eoscpp</code> which can be used to bootstrap a new contract. For this to work we assume you have installed the eosio/eos code installed and that ${CMAKE_INSTALL_PREFIX}/bin is in your path.</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eoscpp -n hello</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;$ cd hello</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;$ ls</div></div><!-- fragment --><p>The above will create a new empty project in the './hello' folder with three files:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;hello.abi hello.hpp hello.cpp</div></div><!-- fragment --><p>Let's take a look at the simplest possible contract:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;cat hello.cpp</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;#include &lt;hello.hpp&gt;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;/**</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160; *  The init() and apply() methods must have C calling convention so that the blockchain can lookup and</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160; *  call these methods.</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160; */</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;extern &quot;C&quot; {</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;    /**</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;     *  This method is called once when the contract is published or updated.</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;     */</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;    void init()  {</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;       eosio::print( &quot;Init World!\n&quot; );</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;    }</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    /// The apply method implements the dispatch of events to this contract</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;    void apply( uint64_t code, uint64_t action ) {</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;       eosio::print( &quot;Hello World: &quot;, eosio::Name(code), &quot;-&gt;&quot;, eosio::Name(action), &quot;\n&quot; );</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;    }</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;} // extern &quot;C&quot;</div></div><!-- fragment --><p>This contract implements the two entry points, <code>init</code> and <code>apply</code>. All it does is log the messages delivered and makes no other checks. Anyone can deliver any message at any time provided the block producers allow it. Absent any required signatures, the contract will be billed for the bandwidth consumed.</p>
<p>You can compile this contract into a text version of WASM (.wast) like so:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eoscpp -o hello.wast hello.cpp</div></div><!-- fragment --><h3>Deploying your Contract</h3>
<p>Now that you have compiled your application it is time to deploy it. This will require you to do the following first:</p>
<ol type="1">
<li>start eosd with wallet plugin enabled</li>
<li>create a wallet &amp; import keys for at least one account</li>
<li>keep your wallet unlocked</li>
</ol>
<p>Assuming your wallet is unlocked and has keys for <code>${account}</code>, you can now upload this contract to the blockchain with the following command:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eosc set contract ${account} hello.wast hello.abi</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Reading WAST...</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Assembling WASM...</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;Publishing contract...</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;{</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  &quot;transaction_id&quot;: &quot;1abb46f1b69feb9a88dbff881ea421fd4f39914df769ae09f66bd684436443d5&quot;,</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;  &quot;processed&quot;: {</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;    &quot;refBlockNum&quot;: 144,</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;    &quot;refBlockPrefix&quot;: 2192682225,</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;    &quot;expiration&quot;: &quot;2017-09-14T05:39:15&quot;,</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;    &quot;scope&quot;: [</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;      &quot;eos&quot;,</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;      &quot;${account}&quot;</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;    ],</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;    &quot;signatures&quot;: [</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;      &quot;2064610856c773423d239a388d22cd30b7ba98f6a9fbabfa621e42cec5dd03c3b87afdcbd68a3a82df020b78126366227674dfbdd33de7d488f2d010ada914b438&quot;</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;    ],</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    &quot;messages&quot;: [{</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;        &quot;code&quot;: &quot;eos&quot;,</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        &quot;type&quot;: &quot;setcode&quot;,</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        &quot;authorization&quot;: [{</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;            &quot;account&quot;: &quot;${account}&quot;,</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;            &quot;permission&quot;: &quot;active&quot;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;          }</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;        ],</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        &quot;data&quot;: &quot;0000000080c758410000f1010061736d0100000001110460017f0060017e0060000060027e7e00021b0203656e76067072696e746e000103656e76067072696e7473000003030202030404017000000503010001071903066d656d6f7279020004696e69740002056170706c7900030a20020600411010010b17004120100120001000413010012001100041c00010010b0b3f050041040b04504000000041100b0d496e697420576f726c64210a000041200b0e48656c6c6f20576f726c643a20000041300b032d3e000041c0000b020a000029046e616d6504067072696e746e0100067072696e7473010004696e697400056170706c790201300131010b4163636f756e744e616d65044e616d6502087472616e7366657200030466726f6d0b4163636f756e744e616d6502746f0b4163636f756e744e616d6506616d6f756e740655496e743634076163636f756e740002076163636f756e74044e616d650762616c616e63650655496e74363401000000b298e982a4087472616e736665720100000080bafac6080369363401076163636f756e7400076163636f756e74&quot;</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;      }</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;    ],</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;    &quot;output&quot;: [{</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;        &quot;notify&quot;: [],</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;        &quot;deferred_transactions&quot;: []</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;      }</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;    ]</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;  }</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;}</div></div><!-- fragment --><p>If you are monitoring the output of your eosd process you should see:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;...] initt generated block #188249 @ 2017-09-13T22:00:24 with 0 trxs  0 pending</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Init World!</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Init World!</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;Init World!</div></div><!-- fragment --><p>You will notice the lines "Init World!" are executed 3 times, this isn't a mistake. When the blockchain is processing transactions the following happens:</p>
<ol type="1">
<li>eosd receives a new transaction<ul>
<li>creates a temporary session</li>
<li>attempts to apply the transaction</li>
<li><b>succeeds and prints "Init World!"</b></li>
<li>or fails undoes the changes (potentially failing after printing "Init World!")</li>
</ul>
</li>
<li>eosd starts to produce a block<ul>
<li>undoes all pending state</li>
<li>pushes all transactions as it builds the block</li>
<li><b>prints "Init World!" a second time</b></li>
<li>finishes building the block</li>
<li>undoes all of the temporary changes while creating block</li>
</ul>
</li>
<li>eosd pushes the generated block as if it received it from the network<ul>
<li><b>prints "Init World!" a third time</b></li>
</ul>
</li>
</ol>
<p>At this point your contract is ready to start receiving messages. Since the default message handler accepts all messages we can send it anything we want. Let's try sending it an empty message:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eosc push message ${account} hello &#39;&quot;abcd&quot;&#39; --scope ${account}</div></div><!-- fragment --><p>This command will send the message "hello" with binary data represented by the hex string "abcd". Note, in a bit we will show how to define the ABI so that you can replace the hex string with a pretty, easy-to-read, JSON object. For now we merely want to demonstrate how the message type "hello" is dispatched to account.</p>
<p>The result is: </p><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;{</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  &quot;transaction_id&quot;: &quot;69d66204ebeeee68c91efef6f8a7f229c22f47bcccd70459e0be833a303956bb&quot;,</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;  &quot;processed&quot;: {</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;    &quot;refBlockNum&quot;: 57477,</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;    &quot;refBlockPrefix&quot;: 1051897037,</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;    &quot;expiration&quot;: &quot;2017-09-13T22:17:04&quot;,</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;    &quot;scope&quot;: [</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;      &quot;${account}&quot;</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;    ],</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;    &quot;signatures&quot;: [],</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;    &quot;messages&quot;: [{</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;        &quot;code&quot;: &quot;${account}&quot;,</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;        &quot;type&quot;: &quot;hello&quot;,</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;        &quot;authorization&quot;: [],</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;        &quot;data&quot;: &quot;abcd&quot;</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;      }</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;    ],</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    &quot;output&quot;: [{</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;        &quot;notify&quot;: [],</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        &quot;deferred_transactions&quot;: []</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;      }</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    ]</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;  }</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;}</div></div><!-- fragment --><p>If you are following along in <code>eosd</code> then you should have seen the following scroll by the screen:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;Hello World: ${account}-&gt;hello</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Hello World: ${account}-&gt;hello</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Hello World: ${account}-&gt;hello</div></div><!-- fragment --><p>Once again your contract was executed and undone twice before being applied the 3rd time as part of a generated block.</p>
<h3>Message Name Restrictions</h3>
<p>Message types (eg. "hello") are actually base32 encoded 64 bit integers. This means they are limited to the charcters a-z, 1-5, and '.' for the first 12 charcters and if there is a 13th character then it is restricted to the first 16 characters ('.' and a-p).</p>
<h3>ABI - Application Binary Interface</h3>
<p>The ABI is a JSON-based description on how to convert user actions between their JSON and Binary representations. The ABI also describes how to convert the database state to/from JSON. Once you have described your contract via an ABI then developers and users will be able to interact with your contract seamlessly via JSON.</p>
<p>We are working on tools that will automate the generation of the ABI from the C++ source code, but for the time being you may have to generate it manually.</p>
<p>Here is an example of what the skeleton contract ABI looks like:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;{</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  &quot;types&quot;: [{</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;      &quot;newTypeName&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;      &quot;type&quot;: &quot;Name&quot;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;    }</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  ],</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;  &quot;structs&quot;: [{</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;      &quot;name&quot;: &quot;transfer&quot;,</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;      &quot;base&quot;: &quot;&quot;,</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;      &quot;fields&quot;: {</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;        &quot;from&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;        &quot;to&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;        &quot;amount&quot;: &quot;UInt64&quot;</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;      }</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;    },{</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;      &quot;name&quot;: &quot;account&quot;,</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;      &quot;base&quot;: &quot;&quot;,</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;      &quot;fields&quot;: {</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;        &quot;account&quot;: &quot;Name&quot;,</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        &quot;balance&quot;: &quot;UInt64&quot;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;      }</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;    }</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;  ],</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;  &quot;actions&quot;: [{</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;      &quot;action&quot;: &quot;transfer&quot;,</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;      &quot;type&quot;: &quot;transfer&quot;</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;    }</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;  ],</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;  &quot;tables&quot;: [{</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;      &quot;table&quot;: &quot;account&quot;,</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;      &quot;type&quot;: &quot;account&quot;,</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;      &quot;indextype&quot;: &quot;i64&quot;,</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;      &quot;keynames&quot; : [&quot;account&quot;],</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;      &quot;keytypes&quot; : [&quot;Name&quot;]</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;    }</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;  ]</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;}</div></div><!-- fragment --><p>You will notice that this ABI defines an action <code>transfer</code> of type <code>transfer</code>. This tells EOS.IO that when <code>${account}-&gt;transfer</code> message is seen that the payload is of type <code>transfer</code>. The type <code>transfer</code> is defined in the <code>structs</code> array in the object with <code>name</code> set to <code>"transfer"</code>.</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;...</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  &quot;structs&quot;: [{</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;      &quot;name&quot;: &quot;transfer&quot;,</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;      &quot;base&quot;: &quot;&quot;,</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;      &quot;fields&quot;: {</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;        &quot;from&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;        &quot;to&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;        &quot;amount&quot;: &quot;UInt64&quot;</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;      }</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;    },{</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;...</div></div><!-- fragment --><p>It has several fields, including <code>from</code>, <code>to</code> and <code>amount</code>. These fields have the coresponding types <code>AccountName</code>, <code>AccountName</code>, and <code>UInt64</code>. <code>AccountName</code> is defined as a typedef in the <code>types</code> array to <code>Name</code>, which is a built in type used to encode a uint64_t as base32 (eg account names).</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;{</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;  &quot;types&quot;: [{</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;      &quot;newTypeName&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;      &quot;type&quot;: &quot;Name&quot;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;    }</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;  ],</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;...</div></div><!-- fragment --><p>Now that we have reviewed the ABI defined by the skeleton, we can construct a message call for <code>transfer</code>:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;eosc push message ${account} transfer &#39;{&quot;from&quot;:&quot;currency&quot;,&quot;to&quot;:&quot;inita&quot;,&quot;amount&quot;:50}&#39; --scope initc</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;2570494ms thread-0   main.cpp:797                  operator()           ] Converting argument to binary...</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;{</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;  &quot;transaction_id&quot;: &quot;b191eb8bff3002757839f204ffc310f1bfe5ba1872a64dda3fc42bfc2c8ed688&quot;,</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;  &quot;processed&quot;: {</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;    &quot;refBlockNum&quot;: 253,</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;    &quot;refBlockPrefix&quot;: 3297765944,</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;    &quot;expiration&quot;: &quot;2017-09-14T00:44:28&quot;,</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;    &quot;scope&quot;: [</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;      &quot;initc&quot;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;    ],</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;    &quot;signatures&quot;: [],</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;    &quot;messages&quot;: [{</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;        &quot;code&quot;: &quot;initc&quot;,</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;        &quot;type&quot;: &quot;transfer&quot;,</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;        &quot;authorization&quot;: [],</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;        &quot;data&quot;: {</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;          &quot;from&quot;: &quot;currency&quot;,</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;          &quot;to&quot;: &quot;inita&quot;,</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;          &quot;amount&quot;: 50</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        },</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;        &quot;hex_data&quot;: &quot;00000079b822651d000000008040934b3200000000000000&quot;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;      }</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    ],</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;    &quot;output&quot;: [{</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        &quot;notify&quot;: [],</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        &quot;deferred_transactions&quot;: []</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;      }</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;    ]</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;  }</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;}</div></div><!-- fragment --><p>If you observe the output of <code>eosd</code> you should see:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;Hello World: ${account}-&gt;transfer</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Hello World: ${account}-&gt;transfer</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Hello World: ${account}-&gt;transfer</div></div><!-- fragment --><h2>Processing Arguments of Transfer Message</h2>
<p>According to the ABI the transfer message has the format:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;&quot;fields&quot;: {</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;    &quot;from&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;    &quot;to&quot;: &quot;AccountName&quot;,</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;    &quot;amount&quot;: &quot;UInt64&quot;</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;}</div></div><!-- fragment --><p>We also know that AccountName -&gt; Name -&gt; UInt64 which means that the binary representation of the message is the same as:</p>
<div class="fragment"><div class="line"><span class="keyword">struct </span>transfer {</div><div class="line">    <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> from;</div><div class="line">    <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> to;</div><div class="line">    <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> amount;</div><div class="line">};</div></div><!-- fragment --><p>The EOS.IO C API provides access to the message payload via the Message API:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;uint32_t messageSize();</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;uint32_t readMessage( void* msg, uint32_t msglen );</div></div><!-- fragment --><p>Let's modify <code>hello.cpp</code> to print out the content of the message:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;hello.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a class="code" href="currency_8cpp.html#a02fd73d861ef2e4aabb38c0c9ff82947">init</a>()  {</div><div class="line">       <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Init World!\n&quot;</span> );</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">struct </span>transfer {</div><div class="line">       <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> from;</div><div class="line">       <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> to;</div><div class="line">       <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> amount;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a class="code" href="currency_8cpp.html#a3c01edc4ed313916967237c16feb1446">apply</a>( <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> code, <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> action ) {</div><div class="line">       <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Hello World: &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(code), <span class="stringliteral">&quot;-&gt;&quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(action), <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">       <span class="keywordflow">if</span>( action == <a class="code" href="group__types.html#gaf9c1edb0e0da55ec6ba09f32f6839529">N</a>(transfer) ) {</div><div class="line">          transfer message;</div><div class="line">          static_assert( <span class="keyword">sizeof</span>(message) == 3*<span class="keyword">sizeof</span>(<a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a>), <span class="stringliteral">&quot;unexpected padding&quot;</span> );</div><div class="line">          <span class="keyword">auto</span> read = <a class="code" href="group__messagecapi.html#ga95a7d555722669cdeb72d6d6d1236893">readMessage</a>( &amp;message, <span class="keyword">sizeof</span>(message) );</div><div class="line">          <a class="code" href="group__systemcapi.html#gae2a6ebaf48fe7c69f847ae18f109d09a">assert</a>( read == <span class="keyword">sizeof</span>(message), <span class="stringliteral">&quot;message too short&quot;</span> );</div><div class="line">          <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Transfer &quot;</span>, message.amount, <span class="stringliteral">&quot; from &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(message.from), <span class="stringliteral">&quot; to &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(message.to), <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">       }</div><div class="line">    }</div><div class="line"></div><div class="line">} <span class="comment">// extern &quot;C&quot;</span></div></div><!-- fragment --><p>Then we can recompile and deploy it with:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;eoscpp -o hello.wast hello.cpp </div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;eosc set contract ${account} hello.wast hello.abi</div></div><!-- fragment --><p><code>eosd</code> will call <a class="el" href="currency_8cpp.html#a02fd73d861ef2e4aabb38c0c9ff82947">init()</a> again because of the redeploy</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;Init World!</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Init World!</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Init World!</div></div><!-- fragment --><p>Then we can execute transfer:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eosc push message ${account} transfer &#39;{&quot;from&quot;:&quot;currency&quot;,&quot;to&quot;:&quot;inita&quot;,&quot;amount&quot;:50}&#39; --scope ${account}</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;{</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;  &quot;transaction_id&quot;: &quot;a777539b7d5f752fb40e6f2d019b65b5401be8bf91c8036440661506875ba1c0&quot;,</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;  &quot;processed&quot;: {</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;    &quot;refBlockNum&quot;: 20,</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;    &quot;refBlockPrefix&quot;: 463381070,</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;    &quot;expiration&quot;: &quot;2017-09-14T01:05:49&quot;,</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;    &quot;scope&quot;: [</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;      &quot;${account}&quot;</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;    ],</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;    &quot;signatures&quot;: [],</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;    &quot;messages&quot;: [{</div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;        &quot;code&quot;: &quot;${account}&quot;,</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;        &quot;type&quot;: &quot;transfer&quot;,</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;        &quot;authorization&quot;: [],</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;        &quot;data&quot;: {</div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;          &quot;from&quot;: &quot;currency&quot;,</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;          &quot;to&quot;: &quot;inita&quot;,</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;          &quot;amount&quot;: 50</div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;        },</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;        &quot;hex_data&quot;: &quot;00000079b822651d000000008040934b3200000000000000&quot;</div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;      }</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;    ],</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    &quot;output&quot;: [{</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;        &quot;notify&quot;: [],</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;        &quot;deferred_transactions&quot;: []</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;      }</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;    ]</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;  }</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;}</div></div><!-- fragment --><p>And on <code>eosd</code> we should see the following output:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;Hello World: ${account}-&gt;transfer</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;Transfer 50 from currency to inita</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;Hello World: ${account}-&gt;transfer</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;Transfer 50 from currency to inita</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;Hello World: ${account}-&gt;transfer</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;Transfer 50 from currency to inita</div></div><!-- fragment --><h3>Using C++ API to Read Messages</h3>
<p>So far we used the C API because it is the lowest level API that is directly exposed by EOS.IO to the WASM virtual machine. Fortunately, eoslib provides a higher level API that removes much of the boiler plate.</p>
<div class="fragment"><div class="line"><span class="keyword">namespace </span><a class="code" href="namespaceeos.html">eos</a> {</div><div class="line">     <span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt;</div><div class="line">     T <a class="code" href="group__messagecppapi.html#ga82362482cd5c990d4b25708a96f72c32">currentMessage</a>();</div><div class="line">}</div></div><!-- fragment --><p>We can update <code>hello.cpp</code> to be more concise as follows:</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;hello.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">extern</span> <span class="stringliteral">&quot;C&quot;</span> {</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a class="code" href="currency_8cpp.html#a02fd73d861ef2e4aabb38c0c9ff82947">init</a>()  {</div><div class="line">       <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Init World!\n&quot;</span> );</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">struct </span>transfer {</div><div class="line">       <a class="code" href="structeos_1_1_name.html">eosio::Name</a> from;</div><div class="line">       <a class="code" href="structeos_1_1_name.html">eosio::Name</a> to;</div><div class="line">       <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> amount;</div><div class="line">    };</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> <a class="code" href="currency_8cpp.html#a3c01edc4ed313916967237c16feb1446">apply</a>( <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> code, <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> action ) {</div><div class="line">       <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Hello World: &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(code), <span class="stringliteral">&quot;-&gt;&quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(action), <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">       <span class="keywordflow">if</span>( action == <a class="code" href="group__types.html#gaf9c1edb0e0da55ec6ba09f32f6839529">N</a>(transfer) ) {</div><div class="line">          <span class="keyword">auto</span> message = eosio::currentMessage&lt;transfer&gt;();</div><div class="line">          <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Transfer &quot;</span>, message.amount, <span class="stringliteral">&quot; from &quot;</span>, message.from, <span class="stringliteral">&quot; to &quot;</span>, message.to, <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">       }</div><div class="line">    }</div><div class="line"></div><div class="line">} <span class="comment">// extern &quot;C&quot;</span></div></div><!-- fragment --><p>You will notice that we updated the <code>transfer</code> struct to use the <code><a class="el" href="structeos_1_1_name.html" title="wraps a uint64_t to ensure it is only passed to methods that expect a Name ">eosio::Name</a></code> type directly, and then condenced the checks around <code>readMessage</code> to a single call to <code>currentMessage</code>.</p>
<p>After compiling and uploading it you should get the same results as the C version.</p>
<h2>Requiring Sender Authority to Transfer</h2>
<p>One of the most common requirements of any contract is to define who is allowed to perform the action. In the case of a curency transfer, we want require that the account defined by the <code>from</code> parameter signs off on the message.</p>
<p>The EOS.IO software will take care of enforcing and validating the signatures, all you need to do is require the necessary authority.</p>
<div class="fragment"><div class="line">...</div><div class="line">void <a class="code" href="currency_8cpp.html#a3c01edc4ed313916967237c16feb1446">apply</a>( <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> code, <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> action ) {</div><div class="line">   <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Hello World: &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(code), <span class="stringliteral">&quot;-&gt;&quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(action), <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">   <span class="keywordflow">if</span>( action == <a class="code" href="group__types.html#gaf9c1edb0e0da55ec6ba09f32f6839529">N</a>(transfer) ) {</div><div class="line">      <span class="keyword">auto</span> message = eosio::currentMessage&lt;transfer&gt;();</div><div class="line">      <a class="code" href="group__messagecapi.html#gaed0ef9b019ed08c7cc0241c5b4169f13">eosio::requireAuth</a>( message.from );</div><div class="line">      <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Transfer &quot;</span>, message.amount, <span class="stringliteral">&quot; from &quot;</span>, message.from, <span class="stringliteral">&quot; to &quot;</span>, message.to, <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">   }</div><div class="line">}</div><div class="line">...</div></div><!-- fragment --><p>After building and deploying we can attempt to transfer again:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160; eosc push message ${account} transfer &#39;{&quot;from&quot;:&quot;initb&quot;,&quot;to&quot;:&quot;inita&quot;,&quot;amount&quot;:50}&#39; --scope ${account}</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160; 1881603ms thread-0   main.cpp:797                  operator()           ] Converting argument to binary...</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160; 1881630ms thread-0   main.cpp:851                  main                 ] Failed with error: 10 assert_exception: Assert Exception</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160; status_code == 200: Error</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160; : 3030001 tx_missing_auth: missing required authority</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160; Transaction is missing required authorization from initb</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;     {&quot;acct&quot;:&quot;initb&quot;}</div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;         thread-0  message_handling_contexts.cpp:19 require_authorization</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;...</div></div><!-- fragment --><p>If you look on <code>eosd</code> you will see this: </p><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;Hello World: initc-&gt;transfer</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;1881629ms thread-0   chain_api_plugin.cpp:60       operator()           ] Exception encountered while processing chain.push_transaction:</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;...</div></div><!-- fragment --><p>This shows that it attempted to apply your transaction, printed the initial "Hello World" and then aborted when <code>eosio::requireAuth</code> failed to find authorization of account <code>initb</code>.</p>
<p>We can fix that by telling <code>eosc</code> to add the required permission:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;eosc push message ${account} transfer &#39;{&quot;from&quot;:&quot;initb&quot;,&quot;to&quot;:&quot;inita&quot;,&quot;amount&quot;:50}&#39; --scope ${account} --permission initb@active</div></div><!-- fragment --><p>The <code>--permission</code> command defines the account and permission level, in this case we use the <code>active</code> authority which is the default.</p>
<p>This time the transfer should have worked like we saw before.</p>
<h2>Aborting a Message on Error</h2>
<p>A large part of contract development is verifying preconditions, such that the amount transferred is greater than 0. If a user attempts to execute an invalid action, then the contract must abort and any changes made get automatically reverted.</p>
<div class="fragment"><div class="line">...</div><div class="line">void <a class="code" href="currency_8cpp.html#a3c01edc4ed313916967237c16feb1446">apply</a>( <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> code, <a class="code" href="group__types.html#gaaa5d1cd013383c889537491c3cfd9aad">uint64_t</a> action ) {</div><div class="line">   <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Hello World: &quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(code), <span class="stringliteral">&quot;-&gt;&quot;</span>, <a class="code" href="structeos_1_1_name.html">eosio::Name</a>(action), <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">   <span class="keywordflow">if</span>( action == <a class="code" href="group__types.html#gaf9c1edb0e0da55ec6ba09f32f6839529">N</a>(transfer) ) {</div><div class="line">      <span class="keyword">auto</span> message = eosio::currentMessage&lt;transfer&gt;();</div><div class="line">      <a class="code" href="group__systemcapi.html#gae2a6ebaf48fe7c69f847ae18f109d09a">assert</a>( message.amount &gt; 0, <span class="stringliteral">&quot;Must transfer an amount greater than 0&quot;</span> );</div><div class="line">      <a class="code" href="group__messagecapi.html#gaed0ef9b019ed08c7cc0241c5b4169f13">eosio::requireAuth</a>( message.from );</div><div class="line">      <a class="code" href="namespaceeos.html#a1df73defa04d39763089df7faa35a934">eosio::print</a>( <span class="stringliteral">&quot;Transfer &quot;</span>, message.amount, <span class="stringliteral">&quot; from &quot;</span>, message.from, <span class="stringliteral">&quot; to &quot;</span>, message.to, <span class="stringliteral">&quot;\n&quot;</span> );</div><div class="line">   }</div><div class="line">}</div><div class="line">...</div></div><!-- fragment --><p> We can now compile, deploy, and attempt to execute a transfer of 0:</p>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;$ eoscpp -o hello.wast hello.cpp</div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;$ eosc set contract ${account} hello.wast hello.abi</div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;$ eosc push message ${account} transfer &#39;{&quot;from&quot;:&quot;initb&quot;,&quot;to&quot;:&quot;inita&quot;,&quot;amount&quot;:0}&#39; --scope initc --permission initb@active</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;3071182ms thread-0   main.cpp:851                  main                 ] Failed with error: 10 assert_exception: Assert Exception</div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;status_code == 200: Error</div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;: 10 assert_exception: Assert Exception</div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;test: assertion failed: Must transfer an amount greater than 0</div></div><!-- fragment --> </div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
